package com.game_info.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.game_info.mapper.GamesMapper;
import com.game_info.service.GamesService;
import com.game_info.vo.Games;
import com.game_info.vo.gImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * games表Service业务层处理
 *
 * @author ttj
 * @date 2020-04-09
 */
@Service
public class GamesServiceImpl implements GamesService {
    @Autowired
    private GamesMapper gamesMapper;

    private ObjectMapper mapper = new ObjectMapper();

    /**
     * 查询单条
     *
     * @param gId 游戏ID
     * @return games对象
     */
    @Override
    public Games selectGamesById(Integer gId) {
        return gamesMapper.selectGamesById(gId);
    }

    /**
     * 查询全部游戏列表
     *
     * @param games
     * @return games集合
     */
    @Override
    public List<Games> selectGamesList(Games games) {
        return gamesMapper.selectGamesList(games);
    }

    /**
     * 根据关键词模糊查询全部字段信息
     *
     * @param keyword
     * @return
     */
    @Override
    public List<Games> selectLikeKeyword(String keyword) {
        return gamesMapper.selectLikeKeyword(keyword);
    }

    /**
     * 分页查询
     *
     * @param startPages
     * @param pagesSize
     * @return
     */
    @Override
    public List<Games> selectGamesListLimit(Integer startPages, Integer pagesSize) {
        return gamesMapper.selectGamesListLimit(startPages, pagesSize);
    }

    /**
     * 获取数据总数
     *
     * @return
     */
    @Override
    public Integer selectListCount() {
        return gamesMapper.selectListCount();
    }

    /**
     * 根据id修改预览图路径
     *
     * @param gImage
     * @param gId
     * @return
     */
    @Override
    public Integer updateGimageById(gImage gImage, Integer gId) throws JsonProcessingException {
            String gImageStr=mapper.writeValueAsString(gImage);//将图片对象转换成json字符串存入数据库
        return gamesMapper.updateGimageById(gImageStr,gId);
    }

    /**
     * 新增
     *
     * @param games
     * @return 结果
     */
    @Override
    public int insertGames(Games games) {
        return gamesMapper.insertGames(games);
    }

    /**
     * 修改
     *
     * @param games
     * @return 结果
     */
    @Override
    public int updateGames(Games games) {
        return gamesMapper.updateGames(games);
    }

    /**
     * 批量删除
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteGamesByIds(String ids) {
        return gamesMapper.deleteGamesByIds(ids.split(""));
    }

    /**
     * 删除
     *
     * @param gId
     * @return 结果
     */
    @Override
    public int deleteGamesById(Integer gId) {
        return gamesMapper.deleteGamesById(gId);
    }
}
